package com.likou;

import java.util.List;

/**
 * https://leetcode-cn.com/problems/add-two-numbers/
 */
public class 两数相加 {
    public static void main(String[] args) {
        System.out.println(Math.pow(10,0));
        ListNode l1 = new ListNode(9);
        ListNode l2 = new ListNode(9);
        ListNode l3 = new ListNode(9);
        ListNode l4 = new ListNode(9);
        l1.next = l2;l2.next=l3;l3.next=l4;
        ListNode la = new ListNode(1);
        ListNode lb = new ListNode(9);
        ListNode lc = new ListNode(9);
        ListNode ld = new ListNode(9);
        ListNode le = new ListNode(9);
        ListNode lf = new ListNode(9);
        ListNode lg = new ListNode(9);
        la.next=lb;lb.next=lc;lc.next=ld;ld.next=le;le.next=lf;lf.next=lg;
        ListNode listNode = addTwoNumbers2(l4, la);

    }
    public static ListNode addTwoNumbers1(ListNode l1, ListNode l2){
        long num1 = sum(l1);
        long num2 = sum(l2);
        long num3 = num1 + num2;
        ListNode result = new ListNode((int)num3 % 10);
        ListNode temp = result;
        num3 /= 10;
        while (num3 != 0){
            long num = num3 % 10;
            result.next = new ListNode((int)num);
            result = result.next;
            num3 /= 10;
        }
        return temp;
    }
    public static long sum(ListNode l1){
        long value = 0;
        int index = 0;
        while (l1 != null){
            value += l1.val*Math.pow(10,index++);
            l1 = l1.next;
        }
        return value;
    }

    public static ListNode addTwoNumbers2(ListNode l1, ListNode l2){
       ListNode listNode = new ListNode(0);
       ListNode result = listNode;
       int scale = 0;
       while (l1!=null || l2 != null || scale != 0){
           int sum = (l1 !=null ? l1.val : 0) + (l2 != null ? l2.val : 0) + scale;
           scale = sum / 10;
           listNode.next = new ListNode(sum % 10);
           listNode = listNode.next;
           if(l1 != null) l1 = l1.next;
           if(l2 != null) l2 = l2.next;
       }
       return result.next;
    }
}

class ListNode{
    int val;
    ListNode next;

    ListNode(){}
    ListNode(int val){
        this.val = val;
    }
    ListNode (int val,ListNode next){
        this.val = val;
        this.next = next;
    }

}